﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KinectDrawer;
using System.Windows.Media.Imaging;
using ICRWrapper;

namespace KinectAlphaBeat
{
    class DrawingAnalyzer
    {
        private float PERCENTAGE_THRESHOLD_FOR_TRIGGERING_COMMAND = .5F;
        private int LINE_HIGHEST_POINT_HEIGHT_FROM_BOTTOM_IN_PIXEL = 80;
        private ICRWrapper.ICRWrapper _icr = new ICRWrapper.ICRWrapper();

        public event EventHandler<DrawingAnalyzerFoundSymbolEventArgs> DrawingAnalyzerFoundSymbol;

        public void DrawerFinishedDrawing(object sender, DrawerUserDrawingReadyEventArgs args)
        {
            BitmapSource image = args.FinalUserDrawing;
            List<List<System.Drawing.Point>> data = args.DrawingData;

            //init de l'objet de réponse
            DrawingAnalyzerFoundSymbolEventArgs response = new DrawingAnalyzerFoundSymbolEventArgs();

            int pointCounter = 0;
            int pointInLeftZoneCounter = 0;
            int pointInRightZoneCounter = 0;

            foreach(List<System.Drawing.Point> line in data)
            {
                foreach (System.Drawing.Point point in line)
                {
                    pointCounter++;

                    if (this.PointIsInLeftZone(point, args.ImageHeight))
                    {
                        pointInLeftZoneCounter++;
                    }

                    if (this.PointIsInRightZone(point, args.ImageHeight, args.ImageWidth))
                    {
                        pointInRightZoneCounter++;
                    }
                }
            }

            //si 90% des points sont dans la zone de gauche
            if (pointInLeftZoneCounter > (PERCENTAGE_THRESHOLD_FOR_TRIGGERING_COMMAND * pointCounter))
            {
                UserAction action = new UserAction();
                action.UserActionType = UserActionType.Command;
                action.Command = UserActionCommand.EreaseWord;
                response.UserAction = action;
            }//ou 90% des points sont dans la zone de droite
            else if (pointInRightZoneCounter > (PERCENTAGE_THRESHOLD_FOR_TRIGGERING_COMMAND * pointCounter))
            {
                UserAction action = new UserAction();
                action.UserActionType = UserActionType.Command;
                action.Command = UserActionCommand.RemoveLastLetter;
                response.UserAction = action;
            }
            else
            {
                //on cherche des lettres
                string result = _icr.Recognize(new LetterICRWrapper(data));

                //si une lettre est trouvée
                if (result != null && result != String.Empty)
                {
                    UserAction action = new UserAction();
                    action.UserActionType = UserActionType.Letter;
                    action.Letter = result.ToCharArray()[0];
                    response.UserAction = action;
                }
                else //sinon, on signal que ne sait pas ce que l'utilisateur souhaite faire
                {
                    UserAction action = new UserAction();
                    action.UserActionType = UserActionType.Unknown;
                    response.UserAction = action;
                }
                
            }

            if (DrawingAnalyzerFoundSymbol != null)
            {
                DrawingAnalyzerFoundSymbol(this, response);
            }

        }

        private int LeftLineMathFunction(int x, int imageHeight)
        {
            return x + imageHeight - LINE_HIGHEST_POINT_HEIGHT_FROM_BOTTOM_IN_PIXEL;
        }

        private int RightLineMathFunction(int x, int imageHeight, int imageWidth)
        {
            return -x + (imageHeight + imageWidth - LINE_HIGHEST_POINT_HEIGHT_FROM_BOTTOM_IN_PIXEL);
        }
        
        private bool PointIsInLeftZone(System.Drawing.Point point, int imageHeight)
        {
            return !(LeftLineMathFunction(point.X, imageHeight) > point.Y);
        }

        private bool PointIsInRightZone(System.Drawing.Point point, int imageHeight, int imageWidth)
        {
            return !(RightLineMathFunction(point.X, imageHeight, imageWidth) > point.Y);
        }
    }
}
